import { onMounted, onBeforeUnmount } from "vue";

export default {
  mounted(el, binding) {
    const callback = binding.value;
    const resizeHandler = () => {
      requestAnimationFrame(() => {
        callback();
      });
    };
    window.addEventListener("resize", resizeHandler);
    // 保存事件引用
    el._resizeHandler = resizeHandler;
    // 初始化执行一次
    resizeHandler();
  },
  beforeUnmount(el) {
    window.removeEventListener("resize", el._resizeHandler);
  },
};
